1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| #include <iostream> #include <vector> #include <string> using namespace std;
const int N = 1000+50;
vector<string> bookCode; vector<string> userCode;
int check(string s1,string s2){ if(s1.size()==s2.size()&&s1.find(s2)!=string::npos) return -1; if(s1.size()==s2.size()) { int m = s1.size(); for(int i=0;i<m;i++){ if((s1[i]-'0')>(s2[i]-'0')) return 1; else if((s1[i]-'0')<(s2[i]-'0')) return 0; } } return s1.size()>s2.size()?1:0; }
void quick_sort(vector<string> &bookCode,int l,int r){ if(l>=r) return; string x = bookCode[(l+r)>>1]; int xl = x.size(),i=l-1,j=r+1; while(i<j){ do i++;while(check(bookCode[i],x)==0); do j--;while(check(bookCode[j],x)==1); if(i<j) { string tmp = bookCode[i]; bookCode[i] = bookCode[j]; bookCode[j] = tmp; } } quick_sort(bookCode,l,j); quick_sort(bookCode,j+1,r); }
string getUserCode(string userCode){ for(int i=0;i<bookCode.size();i++){ string sub = bookCode[i].size()>userCode.size()?bookCode[i].substr(bookCode[i].size()-userCode.size()):bookCode[i]; if(sub.find(userCode)!=string::npos) return bookCode[i]; } return "-1"; }
int main() { int n,q; cin>>n>>q; string s; while(n--) { cin>>s; bookCode.push_back(s); } quick_sort(bookCode,0,bookCode.size()-1); for(int i=0;i<q;i++){ int x; string y; cin>>x>>y; userCode.push_back(y); } for(int i=0;i<userCode.size();i++) cout << getUserCode(userCode[i]) << endl; return 0; }
|